#include "textinput.h"

#include <QLabel>
#include <QTextLine>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QDebug>
#include <QComboBox>

TextInput::TextInput(QWidget *parent) : QWidget(parent)
{

}

TextInput::TextInput(const QString &labelName, const QString &placeholderText, const QString &textInputModel, QWidget *parent)
 : QWidget(parent), labelName(labelName), placeholderText(placeholderText), textInputModel(textInputModel)
{
    showTextInput();
}
void TextInput::setLabelName(const QString &labelName)
{
    this->labelName = labelName;
    qDebug()<<this->labelName<<endl;
}

void TextInput::setPlaceholderText(const QString &placeholderText)
{
    this->placeholderText = placeholderText;
    qDebug()<<this->placeholderText<<endl;
}

void TextInput::setTextInputModel(const QString& textInputModel)
{
    this->textInputModel = textInputModel;
}

QString& TextInput::getTextInput()
{
    this->textInput = this->lineEdit->text();
    return this->textInput;
}

void TextInput::showTextInput()
{
    label = new QLabel(this);// 设置标签父组件
    label->setText(this->labelName);// 设置标签显示
    label->setFixedSize(80,30);// 设置标签长宽

    lineEdit = new QLineEdit(this);// 设置输入框
    lineEdit->setPlaceholderText(this->placeholderText);// 设置输入提示内容
    lineEdit->setFixedSize(200,30);// 设置输入框的长宽

    if(this->textInputModel == "Normal")lineEdit->setEchoMode(QLineEdit::Normal);
    else if(this->textInputModel == "NoEcho")lineEdit->setEchoMode(QLineEdit::NoEcho);
    else if(this->textInputModel == "Password")lineEdit->setEchoMode(QLineEdit::Password);
    else if(this->textInputModel == "PasswordEchoOnEdit")lineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);

    layout = new QHBoxLayout;// 创建水平布局
    layout->setParent(this);// 设置当前父组件
    layout->addStretch();// 添加左弹簧
    layout->addWidget(label);// 添加标签
    layout->addWidget(lineEdit);// 添加输入框
    layout->addStretch();// 添加右弹簧
    setLayout(layout);
}

// 在 TextInput.cpp 文件中实现 clear() 方法
void TextInput::clear()
{
    lineEdit->clear();
}

TextInput::~TextInput()
{
    delete label;
    delete lineEdit;
    delete layout;
}
